package main


import (
"fmt"
"context"
"time"
"sync"
)

func main() {
    messages := make(chan int, 10)

    // producer
    for i := 0; i < 10; i++ {
        messages <- i
    }

    wg := sync.WaitGroup{}

    wg.Add(1)

    ctx, cancel := context.WithCancel(context.Background())

    // consumer
    go func(ctx context.Context) {
        ticker := time.NewTicker(1 * time.Second)
        for _ = range ticker.C {
            select {
            case <-ctx.Done():
                xq , ts := context.WithCancel(ctx)
                    
                fmt.Println("child process interrupt...")
                wg.Done()
                return
            default:
                fmt.Printf("send message: %d\n", <-messages)
            }
        }
        
    }(ctx)

    defer close(messages)
    
    time.Sleep(3 * time.Second)

    cancel()

    wg.Wait()
    fmt.Println("ok")
}